﻿using BusinessLayer;
using BusinessLayer.VDataEntities;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using TrialManagementTool.Helper;
using TrialManagmentTool.Models;
namespace TrialManagementTool.Controllers
{
    public class HomeController : Controller
    {
        [Authentication]
        public ActionResult Index()
        {
            return View();
        }

        [Authentication]
        public ActionResult Detail(int ID)
        {
            TrialDetailViewModel model = new TrialDetailViewModel();
            VTrialDetail trialDetail = TrialBusiness.GetTrialDetail(ID);
            if (trialDetail.TrialSite.status == (int)TrialSiteStatus.Deleted)
            {
                return View("DeleteTrialSite");
            }
            List<VReport> reports = trialDetail.Reports.ToList();
            List<ProcessViewModel> processModel = new List<ProcessViewModel>();
            for (int j = 0; j < reports.Count(); j++)
            {
                ProcessViewModel perProcessItem = new ProcessViewModel();
                perProcessItem.NonObserveDataList = new List<VReport>();
                perProcessItem.ObserveDataList = new List<VReport>();

                perProcessItem.reportName = reports[j].Name;
                List<VReportInfo> reportInfoYongyao = ReportBusiness.GetProcessInfoByProcessID(reports[j].ID, false);
                for (int i = 0; i < reportInfoYongyao.Count(); i++)
                {
                    VReport vReportitem = ReportBusiness.GetByID(reportInfoYongyao[i].ID);
                    vReportitem.headTitle = string.Format("第{0}次用药", Common.ConvertNumToChinese(i + 1));
                    perProcessItem.NonObserveDataList.Add(vReportitem);
                }
                List<VReportInfo> reportInfoGuancha = ReportBusiness.GetProcessInfoByProcessID(reports[j].ID, true);
                for (int i = 0; i < reportInfoGuancha.Count(); i++)
                {
                    VReport vReportitem = ReportBusiness.GetByID(reportInfoGuancha[i].ID);
                    vReportitem.headTitle = string.Format("第{0}次调查", Common.ConvertNumToChinese(i + 1));
                    perProcessItem.ObserveDataList.Add(vReportitem);
                }
                processModel.Add(perProcessItem);
            }
            model.ProcessViewModelDataList = processModel;
            model.trial = trialDetail;
            if (trialDetail.TrialGuideID > 0)
            {
                model.trialGuide = TrialBusiness.GetTrialGuide(trialDetail.TrialGuideID.Value);
                model.isExistTrialGuide = true;
            }
            else
            {
                model.isExistTrialGuide = false;
            }
            if (trialDetail.TrialSite.CreateDate.HasValue)
            {
                model.CreateDateTime = trialDetail.TrialSite.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (trialDetail.TrialSite.CompleteDate.HasValue)
            {
                model.FinishDateTime = trialDetail.TrialSite.CompleteDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (trialDetail.TrialSite.SubmitDate.HasValue)
            {
                model.SubmitDateTime = trialDetail.TrialSite.SubmitDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (trialDetail.TrialSite.LastObserveDate.HasValue)
            {
                model.LastObserveDateTime = trialDetail.TrialSite.LastObserveDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }

            if (trialDetail.TrialSite == null)
            {
                model.isExistBasicInfo = true;
            }
            else
            {
                model.isExistBasicInfo = false;
            }
            if (trialDetail.ProjectCategory == null || trialDetail.TTM == null || string.IsNullOrEmpty(trialDetail.CA) || string.IsNullOrEmpty(trialDetail.FSM))
            {
                model.isShowEditButton = true;
            }
            else
            {
                model.isShowEditButton = false;
            }
            if (trialDetail.ProjectCategory != null)
            {
                model.ProjectCategoryID = trialDetail.ProjectCategory.ID;
            }
            if (trialDetail.TTM != null)
            {
                model.TTMID = trialDetail.TTM.ID;
            }
            List<DynamicInfoSummary> mainData = new List<DynamicInfoSummary>();
            mainData.Add(new DynamicInfoSummary { name = "Address", display = true, value = trialDetail.TrialSite.Address, isMandary = true, ChineseDesc = "市县/乡镇" });
            mainData.Add(new DynamicInfoSummary { name = "Partner", display = true, value = trialDetail.TrialSite.Partner, isMandary = false, ChineseDesc = "合作单位" });
            mainData.Add(new DynamicInfoSummary { name = "CropCategory", display = true, value = trialDetail.TrialSite.CropCategory, isMandary = true, ChineseDesc = "作物品种" });
            mainData.Add(new DynamicInfoSummary { name = "SeedMode", display = true, value = trialDetail.TrialSite.SeedMode, isMandary = true, ChineseDesc = "栽培模式" });
            mainData.Add(new DynamicInfoSummary { name = "Object", display = true, value = trialDetail.TrialSite.Object, isMandary = true, ChineseDesc = "防治靶标" });
            string temp = "";
            if (trialDetail.TrialSite.IsClosed.HasValue && trialDetail.TrialSite.IsClosed.Value == true) { temp = "True"; }
            else if (trialDetail.TrialSite.IsClosed.HasValue && trialDetail.TrialSite.IsClosed.Value == false)
            {
                temp = "False";
            }
            mainData.Add(new DynamicInfoSummary { name = "IsClosed", display = true, value = temp, isMandary = true, ChineseDesc = "是否封闭" });
            mainData.Add(new DynamicInfoSummary { name = "SubCropCategory", display = true, value = trialDetail.TrialSite.SubCropCategory, isMandary = true, ChineseDesc = "季节" });
            switch (trialDetail.TrialSite.EffectResult)
            {
                case "1":
                    trialDetail.TrialSite.EffectResult = "不能接受";
                    break;
                case "2":
                    trialDetail.TrialSite.EffectResult = "有效";
                    break;
                case "3":
                    trialDetail.TrialSite.EffectResult = "效果好";
                    break;
                case "4":
                    trialDetail.TrialSite.EffectResult = "效果优异";
                    break;
                default:
                    trialDetail.TrialSite.EffectResult = "";
                    break;
            }
            switch (trialDetail.ProductCategory.Name)
            {
                case "除草剂":
                    break;
                case "杀菌剂":
                    mainData.Find(c => c.name.Equals("Object")).ChineseDesc = "靶标病害";
                    mainData.Find(c => c.name.Equals("SeedMode")).ChineseDesc = "种植模式";
                    mainData.Find(c => c.name.Equals("IsClosed")).display = false;

                    break;
                case "杀虫剂":
                    mainData.Find(c => c.name.Equals("Object")).ChineseDesc = "靶标害虫";
                    mainData.Find(c => c.name.Equals("SeedMode")).ChineseDesc = "种植模式";
                    mainData.Find(c => c.name.Equals("IsClosed")).display = false;
                    break;
                case "肥料增效剂":
                    mainData.Find(c => c.name.Equals("Object")).ChineseDesc = "靶标杂草/病害/害虫";
                    mainData.Find(c => c.name.Equals("SeedMode")).ChineseDesc = "种植模式";
                    mainData.Find(c => c.name.Equals("IsClosed")).display = false;
                    break;
            }
            model.BasicInfoList = mainData.Where(c => c.display == true).ToList();
            return View(model);
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Logout()
        {
            Session.RemoveAll();
            Session.Clear();
            Session.Abandon();
            UserHelper.DeleteUserInfoCookie();
            return View("Login");
        }
    }
}
